<?php
/**
 * UsersModel
 * @author   songhuan <iphper@yeah.net>
 * @version   $Id: UsersModel.php 1 2011-06-20 01:11:00Z $
 */
class UsersModel extends CMysqlTable
{
	public static function model($className = __CLASS__)
	{
		return parent::model($className);
	}
	
	public $relations = array(
		'Groups' => array(self::BELONGS_TO, 'Groups', 'group_id'),
	);
	
	public $rules = array(
		'user_loginname' => array(
			'required' => true,	
			'minlength' => 6, 
			'maxlength' => 12, 
			'unique' => true, 
			'when' => self::INSERT
		),
		'user_password' => array(
			'required' => true,	
			'minlength' => 6, 
			'maxlength' => 16, 
			'when' => self::BOTH
		),
		'user_repassword' => array(
			'required' => true, 
			'equalTo' => 'user_password', 
			'when' => self::BOTH
		),
		'user_email' => array(
			'required' => true, 
			'email' => true, 
			'when' => self::BOTH
		)
	);
	
	public $messages = array(
		'user_loginname' => array(
			'required' => '请填写登录名',
			'minlength' => '登录名长度由大于6个英文字母、数字、.或_组成',
			'maxlength' => '登录名长度由小于12个英文字母、数字、.或_组成',
			'unique' => '此登录名已被使用，请更换登录名'
		),
		'user_password' => array(
			'required' => '请填写密码',
			'minlength' => '密码长度由大于6个英文字母、数字、或特殊符号组成',
			'maxlength' => '密码长度由小于16个英文字母、数字、或特殊符号组成'
		),
		'user_repassword' => array(
			'required' => '请再次输入密码',
			'equalTo' => '两次密码输入不一致'
		),
		'user_email' => array(
			'required' => '请填写邮箱，可以通过邮箱找回密码',
			'email' => '邮箱格式不正确'
		)
	);
	
	public function preInsert()
	{
		$this->ip_last_login = CRequest::getInstance()->clientIp;
		$this->dt_registered = $this->dt_last_login = date('Y-m-d H:i:s');
		$this->user_password = md5($this->user_password);
		return true;
	}
	
	public function preUpdate()
	{
		if (isset($this->user_password))
			$this->user_password = md5($this->user_password);
		
		return true;
	}
}
